package com.robusta.app.auth.persistence;

import com.robusta.app.auth.model.UserModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Repository
public class UserRepositoryJpa implements UserRepository {
    @PersistenceContext(unitName = "authentication-authorization-persistence")
    private EntityManager entityManager;
    private static final Logger LOGGER = LoggerFactory.getLogger(UserRepositoryJpa.class);

    public UserModel lookupUserByCredentials(String loginName, String encodedPassword) {
        return entityManager.createQuery(
                    "SELECT user FROM UserModel user where user.loginName = :loginName and password = :password", UserModel.class)
                .setParameter("loginName", loginName)
                .setParameter("password", encodedPassword)
                .getSingleResult();
    }
}
